package com.jrx.anytxn.param.service.product.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.common.uitl.BeanMapping;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.number.exception.SegmentException;
import com.jrx.anytxn.number.service.ISegmentService;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.constant.ErrorMsgConstant;
import com.jrx.anytxn.param.dto.product.PrBlockCodeRuleReq;
import com.jrx.anytxn.param.dto.product.PrBlockCodeRuleRes;
import com.jrx.anytxn.param.entity.PrBlockCodeRule;
import com.jrx.anytxn.param.mapper.ext.ExtPrBlockCodeRuleMapper;
import com.jrx.anytxn.param.service.product.IPrBlockCodeRuleService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@CacheConfig(cacheNames = "BLOCK_CODE_RULE")
public class PrBlockCodeRuleServiceImpl implements IPrBlockCodeRuleService {
    private static final Logger logger = LoggerFactory.getLogger(PrBlockCodeRuleServiceImpl.class);
    @Resource
    private ExtPrBlockCodeRuleMapper extPrBlockCodeRuleMapper;
    @Autowired
    private ISegmentService segmentService;

    @Override
    public TxnPage<PrBlockCodeRuleRes> findAllPageByTenantId(Integer pageNum, Integer pageSize, String tenantId) throws TxnException {
        Page<PrBlockCodeRuleRes> page = PageHelper.startPage(pageNum, pageSize, Constant.ORDER_BY_SORT);
        List<PrBlockCodeRule> tableList = extPrBlockCodeRuleMapper.selectAllByTenId(tenantId);
        if (tableList.isEmpty()) {
            logger.error(TxnRespCode.NO_DATA.getMsg());
            throw new TxnBizException(TxnRespCode.NO_DATA.getMsg());
        }
        List<PrBlockCodeRuleRes> res = BeanMapping.copyList(tableList, PrBlockCodeRuleRes.class);
        return new TxnPage<>(pageNum, pageSize, page.getTotal(), res);
    }

    @Cacheable(key = "'blockCodeRule_'+#tenantId+'_'+#id")
    @Override
    public PrBlockCodeRuleRes findByTenIdAndId(String tenantId, Long id) throws TxnBizException {
        if (StringUtils.isBlank(tenantId)) {
            logger.error(TxnRespCode.NOT_EMPTY.getMsg());
            throw new TxnBizException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        PrBlockCodeRule prBlockCodeRuleTable = extPrBlockCodeRuleMapper.selectByTenIdAndId(tenantId, id);
        return BeanMapping.copy(prBlockCodeRuleTable, PrBlockCodeRuleRes.class);
    }

    @Override
    public PrBlockCodeRuleRes addPrBlockCodeRule(PrBlockCodeRuleReq request) throws TxnBizException {
        PrBlockCodeRule table = extPrBlockCodeRuleMapper.selectByTenIdAndCodeAngDelqDays(request.getTenantId(), request.getBlockCode(),request.getDelqDays());
        if (null != table) {
            logger.error(TxnRespCode.DATA_EXISTS.getMsg());
            throw new TxnBizException(TxnRespCode.DATA_EXISTS.getMsg());
        }

        PrBlockCodeRule prBlockCodeRule = BeanMapping.copy(request, PrBlockCodeRule.class);

        try {
            prBlockCodeRule.setId(segmentService.getId(Constant.PR_BLOCK_CODE_RULE));
        } catch (SegmentException e) {
            logger.error("主键生成错误", e);
            throw new TxnBizException(ErrorMsgConstant.ERROR_ID);
        }
        prBlockCodeRule.setRecordVersionNumber(Constant.VERSION_NUMBER);
        extPrBlockCodeRuleMapper.insertSelective(prBlockCodeRule);
        return BeanMapping.copy(prBlockCodeRule, PrBlockCodeRuleRes.class);
    }

    @CacheEvict(key = "'blockCodeRule_'+#request.tenantId+'_'+#request.id")
    @Override
    public PrBlockCodeRuleRes modifyPrBlockCodeRule(PrBlockCodeRuleReq request) throws TxnBizException {
        PrBlockCodeRule table = extPrBlockCodeRuleMapper.selectByTenIdAndCodeAngDelqDays(request.getTenantId(), request.getBlockCode(),request.getDelqDays());
        if (null != table) {
            logger.error(TxnRespCode.DATA_EXISTS.getMsg());
            throw new TxnBizException(TxnRespCode.DATA_EXISTS.getMsg());
        }

        PrBlockCodeRule prBlockCodeRule = BeanMapping.copy(request, PrBlockCodeRule.class);
        // 更新
        extPrBlockCodeRuleMapper.updateByTenIdAndId(prBlockCodeRule);
        return BeanMapping.copy(prBlockCodeRule, PrBlockCodeRuleRes.class);
    }

    @CacheEvict(key = "'blockCodeRule_'+#tenantId+'_'+#id")
    @Override
    public Boolean deletePrBlockCodeRule(String tenantId, Long id) throws TxnBizException {
        if (StringUtils.isBlank(tenantId) ) {
            logger.error(TxnRespCode.NOT_EMPTY.getMsg());
            throw new TxnBizException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        int i = extPrBlockCodeRuleMapper.deleteByTenIdAndId(tenantId, id);
        return i > 0;
    }
}
